//
//  MKUIElementViewBase.m
//  MKUI
//
//  Created by 苏尚进 on 2023/7/10.
//  Copyright © 2023 moon. All rights reserved.
//

#import "MKUIElementViewBase.h"
#import "MKUIPreCompile.h"
#import "UIColor+MKUI.h"
#import "UIImage+MKUI.h"

@interface MKUIElementViewBase ()
@property(nonatomic,strong) UIView *contentView;
@property(nonatomic,strong) UIImageView *iconImageView;//左侧的图标
@property(nonatomic,strong) UILabel *titleLabel;//标题
@property(nonatomic,strong) UILabel *subTitleLabel;//副标题
@property(nonatomic,strong) UIView *disclosureIndicatorAccessView;//向右箭头的accessView
@property(nonatomic,strong) UIView *checkmarkAccessView;//打勾的accessView
@end

@implementation MKUIElementViewBase
+ (instancetype)elementWithIconImage:(nullable UIImage *)iconImage titleText:(nullable NSString *)titleText{
    MKUIElementViewBase *el = [[self alloc] init];
    el.iconImage = iconImage;
    el.titleText = titleText;
    return el;
}
- (id)initWithFrame:(CGRect)frame{
    if(self=[super initWithFrame:frame]){
        self.contentView = [[UIView alloc] init];
        [self addSubview:self.contentView];
        //
        _hiddenEmptyIconImage = YES;
        _iconImageSize = CGSizeMake(28, 28);
        self.contentInsets = UIEdgeInsetsMake(8, 20, 8, 20);
        self.backgroundColor = [UIColor clearColor];
        //
        self.iconImageView = [[UIImageView alloc] init];
        self.iconImageView.contentMode = UIViewContentModeScaleAspectFit;
        self.iconImageView.hidden = self.hiddenEmptyIconImage && self.iconImage==nil;
        [self.contentView addSubview:self.iconImageView];
        //
        self.titleLabel = [[UILabel alloc] init];
        self.titleLabel.font = [UIFont systemFontOfSize:16];
        self.titleLabel.textAlignment = NSTextAlignmentLeft;
        self.titleLabel.numberOfLines = 1;
        self.titleLabel.textColor = [UIColor mk_colorWithLight:[UIColor blackColor]];
        [self.contentView addSubview:self.titleLabel];
        //
        self.subTitleLabel = [[UILabel alloc] init];
        self.subTitleLabel.font = [UIFont systemFontOfSize:11];
        self.subTitleLabel.textAlignment = NSTextAlignmentLeft;
        self.subTitleLabel.numberOfLines = 1;
        self.subTitleLabel.textColor = [UIColor mk_colorWithLight:[UIColor mk_colorWithString:@"#898989"]];
        [self.contentView addSubview:self.subTitleLabel];
        
        @MK_WEAKIFY(self);
        MKUILayoutConstraintItemWrapper *w1 = [MKUILayoutConstraintItemWrapper wrapItem:self.iconImageView sizeThatFitsBlock:^CGSize(MKUILayoutConstraintItemWrapper * _Nonnull wrapper, CGSize size, BOOL resizeItems) {
            @MK_NORMALIZE(self);
            return self.iconImageSize;
        }];
        w1.margin = self.iconImageMarggin;
        self.iconImageViewLayoutConstraint = w1;
        MKUIFlowLayoutConstraint *f2 = [[MKUIFlowLayoutConstraint alloc] initWithItems:@[self.titleLabel,self.subTitleLabel] constraintParam:(MKUIFlowLayoutConstraintParam_V_C_L) contentInsets:UIEdgeInsetsZero interitemSpacing:8];
        self.titlesFlowLayoutConstraint = f2;
        self.segLayoutConstraint = [[MKUISegmentFlowLayoutConstraint alloc] initWithItems:@[w1,f2] constraintParam:(MKUISegmentFlowLayoutConstraint_H_C) contentInsets:UIEdgeInsetsZero interitemSpacing:15];
        self.segLayoutConstraint.boundaryItemIndex = 1;
        self.segLayoutConstraint.isLayoutPriorityFirstItems = YES;
    }
    return self;
}
#ifdef DEBUG
- (void)dealloc{
    
}
#endif
- (void)layoutSubviews{
    [super layoutSubviews];
    CGRect bounds = self.bounds;
    self.contentView.frame = bounds;
    [self layoutContentView];
}
- (void)layoutContentView{
    CGRect bounds = self.bounds;
    UIEdgeInsets insets = [self contentInsetsWithSafeArea];
    if(self.accessView){
        MKUILayoutConstraintItemWrapper *w1 = [MKUILayoutConstraintItemWrapper wrapItem:self.contentView sizeThatFitsBlock:^CGSize(MKUILayoutConstraintItemWrapper * _Nonnull wrapper, CGSize size, BOOL resizeItems) {
            return size;
        }];
        MKUISegmentFlowLayoutConstraint *seg1 = [[MKUISegmentFlowLayoutConstraint alloc] initWithItems:@[w1,self.accessView] constraintParam:(MKUISegmentFlowLayoutConstraint_H_C) contentInsets:insets interitemSpacing:insets.right];
        seg1.bounds = bounds;
        [seg1 layoutItemsWithResizeItems:YES];
    }else{
        CGRect bounds = self.bounds;
        CGRect f1 = UIEdgeInsetsInsetRect(bounds, insets);
        self.contentView.frame = f1;
    }
    if(self.segLayoutConstraint){
        self.segLayoutConstraint.bounds = self.contentView.bounds;
        [self.segLayoutConstraint layoutItemsWithResizeItems:YES];
    }
}
- (CGSize)intrinsicContentSize{
    return [self sizeThatFits:[UIScreen mainScreen].bounds.size];
}
- (CGFloat)titleAndSubTitleHeightThatFits:(CGSize)size{
    CGFloat height = self.subTitleText.length>0&&!self.subTitleLabel.hidden ? 58:44;
    return height;
}
- (CGSize)sizeThatFits:(CGSize)size{
    CGFloat height = [self titleAndSubTitleHeightThatFits:size];
    return CGSizeMake(size.width, height);
}
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection{
    [super traitCollectionDidChange:previousTraitCollection];
    if(_disclosureIndicatorAccessView){
        UIImageView *imageView = (UIImageView *)_disclosureIndicatorAccessView;
        imageView.image = [UIImage mk_disclosureIndicatorImageWithColor:UIColor.mk_disclosureIndicatorColor];
    }
}
- (void)setIconImage:(UIImage *)iconImage{
    if(_iconImage==iconImage) return;
    _iconImage = iconImage;
    self.iconImageView.image = iconImage;
    self.iconImageView.hidden = self.hiddenEmptyIconImage && self.iconImage==nil;
    [self setNeedsLayout];
}
- (void)setHiddenEmptyIconImage:(BOOL)hiddenEmptyIconImage{
    if(_hiddenEmptyIconImage==hiddenEmptyIconImage) return;
    _hiddenEmptyIconImage = hiddenEmptyIconImage;
    self.iconImageView.hidden = self.hiddenEmptyIconImage && self.iconImage==nil;
    [self setNeedsLayout];
}
- (void)setIconImageMarggin:(UIEdgeInsets)iconImageMarggin{
    _iconImageMarggin = iconImageMarggin;
    self.iconImageViewLayoutConstraint.margin = iconImageMarggin;
    [self setNeedsLayout];
}
- (void)setIconImageSize:(CGSize)iconImageSize{
    if(CGSizeEqualToSize(_iconImageSize, iconImageSize))return;
    _iconImageSize = iconImageSize;
    [self setNeedsLayout];
}
- (void)setTitleText:(NSString *)titleText{
    if(_titleText==titleText || [_titleText isEqualToString:titleText]) return;
    _titleText = titleText;
    self.titleLabel.text = titleText;
    [self setNeedsLayout];
}
- (void)setSubTitleText:(NSString *)subTitleText{
    if(_subTitleText==subTitleText || [_subTitleText isEqualToString:subTitleText]) return;
    _subTitleText = subTitleText;
    self.subTitleLabel.text = subTitleText;
    [self setNeedsLayout];
}
- (void)setAccessView:(UIView *)accessView{
    if(_accessView==accessView)return;
    if(_accessView) [_accessView removeFromSuperview];
    _accessView = accessView;
    if(_accessView) [self addSubview:_accessView];
    [self setNeedsLayout];
}
- (void)setContentInsets:(UIEdgeInsets)contentInsets{
    if(UIEdgeInsetsEqualToEdgeInsets(_contentInsets, contentInsets)) return;
    _contentInsets = contentInsets;
    [self setNeedsLayout];
}
- (UIView *)disclosureIndicatorAccessView{
    if(_disclosureIndicatorAccessView) return _disclosureIndicatorAccessView;
    _disclosureIndicatorAccessView = [self disclosureIndicatorAccessViewWithColor:UIColor.mk_disclosureIndicatorColor];
    return _disclosureIndicatorAccessView;
}
- (UIView *)disclosureIndicatorAccessViewWithColor:(UIColor *)color{
    UIImage *image = [UIImage mk_disclosureIndicatorImageWithColor:color];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    imageView.frame = CGRectMake(0, 0, image.size.width, image.size.height);
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    return imageView;
}
- (UIView *)checkmarkAccessView{
    if(_checkmarkAccessView) return _checkmarkAccessView;
    _checkmarkAccessView = [self checkmarkAccessViewWithColor:UIColor.systemBlueColor];
    return _checkmarkAccessView;
}
- (UIView *)checkmarkAccessViewWithColor:(UIColor *)color{
    UIImage *image = [UIImage mk_checkmarkImageWithColor:color];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    imageView.frame = CGRectMake(0, 0, image.size.width, image.size.height);
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    return imageView;
}
- (UIEdgeInsets)contentInsetsWithSafeArea{
    UIEdgeInsets insets = self.contentInsets;
    UIEdgeInsets safeArea = UIEdgeInsetsZero;
    if (@available(iOS 11.0, *)) {
        safeArea = self.safeAreaInsets;
    }
    insets.left += safeArea.left;
    insets.right += safeArea.right;
    insets.top += safeArea.top;
    insets.bottom += safeArea.bottom;
    return insets;
}
- (CGSize)sizeThatFits:(CGSize)size contentSizeBlock:(CGSize(^)(CGSize contentSize))block{
    CGSize s = size;
    UIEdgeInsets insets = [self contentInsetsWithSafeArea];
    if(self.accessView){
        MKUILayoutConstraintItemWrapper *w1 = [MKUILayoutConstraintItemWrapper wrapItem:self.contentView sizeThatFitsBlock:^CGSize(MKUILayoutConstraintItemWrapper * _Nonnull wrapper, CGSize size, BOOL resizeItems) {
            CGSize s = size;
            if(block){
                s = block(CGSizeMake(size.width, 99999999));
            }
            return s;
        }];
        MKUISegmentFlowLayoutConstraint *seg1 = [[MKUISegmentFlowLayoutConstraint alloc] initWithItems:@[w1,self.accessView] constraintParam:(MKUISegmentFlowLayoutConstraint_H_C) contentInsets:insets interitemSpacing:insets.right];
        s = [seg1 sizeThatFits:size resizeItems:YES];
    }else{
        if(block){
            s = block(CGSizeMake(size.width-insets.left-insets.right, 99999999));
        }
        if(s.height>0){
            s.height += insets.top+insets.bottom;
        }
    }
    return s;
}
@end
@implementation UIColor(MKUIElementViewBase)
+ (UIColor *)mk_disclosureIndicatorColor{
    return [UIColor mk_colorWithLight:[UIColor mk_colorWithString:@"#D2D2D2"] dark:[UIColor mk_colorWithString:@"#9C9C9C"]];
}
@end



@interface MKUIElementListViewCellBase(){
    __kindof MKUIElementViewBase *_elementView;
}
@end
@implementation MKUIElementListViewCellBase
- (id)initWithFrame:(CGRect)frame{
    if(self=[super initWithFrame:frame]){
        Class elementViewClass = self.class.elementViewClass;
        if(elementViewClass){
            _elementView = [[elementViewClass alloc] init];
            [self.contentView addSubview:self.elementView];
        }
    }
    return self;
}
- (MKUIElementViewBase *)elementView{
    return _elementView;
}
+ (Class)elementViewClass{
    return MKUIElementViewBase.class;
}
- (void)layoutSubviews{
    [super layoutSubviews];
    CGRect bounse = self.contentView.bounds;
    self.elementView.frame = bounse;
    [self.elementView setNeedsLayout];
    [self.elementView layoutIfNeeded];
}
- (CGSize)sizeThatFits:(CGSize)size{
    return [self.elementView sizeThatFits:size];
}
@end

@implementation MKUIElementTableViewCellModelBase
- (MKUIElementViewBase *)el_elementView{
    if(![self.tableViewCell isKindOfClass:[MKUIElementTableViewCellBase class]]) return nil;
    MKUIElementTableViewCellBase *el = self.tableViewCell;
    return el.elementView;
}
- (UIImage *)el_iconImage{
    return self[@"el_iconImage"];
}
- (void)setEl_iconImage:(UIImage *)el_iconImage{
    self[@"el_iconImage"] = el_iconImage;
    self.el_elementView.iconImage = el_iconImage;
}
- (UIEdgeInsets)el_iconImageMarggin{
    return [self el_iconImageMargginWithOtherWise:UIEdgeInsetsZero];
}
- (UIEdgeInsets)el_iconImageMargginWithOtherWise:(UIEdgeInsets)others{
    NSValue *value = [self mk_NSValueForKeyPath:@"el_iconImageMarggin"];
    if(!value) return others;
    return value.UIEdgeInsetsValue;
}
- (void)setEl_iconImageMarggin:(UIEdgeInsets)el_iconImageMarggin{
    self[@"el_iconImageMarggin"] = [NSValue valueWithUIEdgeInsets:el_iconImageMarggin];
    self.el_elementView.iconImageMarggin = el_iconImageMarggin;
}
- (CGSize)el_iconImageSize{
    return [self el_iconImageSizeWithOtherWise:CGSizeZero];
}
- (CGSize)el_iconImageSizeWithOtherWise:(CGSize)others{
    NSValue *value = [self mk_NSValueForKeyPath:@"el_iconImageSize"];
    if(!value) return others;
    return value.CGSizeValue;
}
- (void)setEl_iconImageSize:(CGSize)el_iconImageSize{
    self[@"el_iconImageSize"] = [NSValue valueWithCGSize:el_iconImageSize];
    self.el_elementView.iconImageSize = el_iconImageSize;
}
- (BOOL)el_hiddenEmptyIconImage{
    return [self el_hiddenEmptyIconImageWithOtherWise:NO];
}
- (BOOL)el_hiddenEmptyIconImageWithOtherWise:(BOOL)others{
    return [self mk_boolForKeyPath:@"el_hiddenEmptyIconImage" otherwise:others];
}
- (void)setEl_hiddenEmptyIconImage:(BOOL)el_hiddenEmptyIconImage{
    self[@"el_hiddenEmptyIconImage"] = @(el_hiddenEmptyIconImage);
    self.el_elementView.hiddenEmptyIconImage = el_hiddenEmptyIconImage;
}
- (NSString *)el_titleText{
    return [self mk_stringForKeyPath:@"el_titleText"];
}
- (void)setEl_titleText:(NSString *)el_titleText{
    self[@"el_titleText"] = el_titleText;
    self.el_elementView.titleText = el_titleText;
}
- (NSString *)el_subTitleText{
    return [self mk_stringForKeyPath:@"el_subTitleText"];
}
- (void)setEl_subTitleText:(NSString *)el_subTitleText{
    self[@"el_subTitleText"] = el_subTitleText;
    self.el_elementView.subTitleText = el_subTitleText;
}
- (UITableViewCellAccessoryType)el_accessoryType{
    return (UITableViewCellAccessoryType)[self mk_integerForKeyPath:@"el_accessoryType"];
}
- (void)setEl_accessoryType:(UITableViewCellAccessoryType)el_accessoryType{
    self[@"el_accessoryType"] = @(el_accessoryType);
}
@end
@interface MKUIElementTableViewCellBase (){
    __kindof MKUIElementViewBase *_elementView;
}
@end
@implementation MKUIElementTableViewCellBase
+ (Class)elementViewClass{
    return [MKUIElementViewBase class];
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        if([self.class elementViewClass]){
            _elementView = [[[self.class elementViewClass] alloc] init];
            [self.contentView addSubview:self.elementView];
        }
    }
    return self;
}

- (MKUIElementViewBase *)elementView{
    return _elementView;
}
- (void)customLayoutSubviews{
    [super customLayoutSubviews];
    CGRect bounse = self.contentView.bounds;
    self.elementView.frame = bounse;
    [self.elementView setNeedsLayout];
    [self.elementView layoutIfNeeded];
}
- (CGSize)customSizeThatFits:(CGSize)size{
    return [self.elementView sizeThatFits:size];
}
- (void)customReloadCellModel{
    [super customReloadCellModel];
    MKUIElementTableViewCellModelBase *cm = self.cellModel;
    self.accessoryType = cm.el_accessoryType;
    self.elementView.iconImage = cm.el_iconImage;
    self.elementView.iconImageSize = [cm el_iconImageSizeWithOtherWise:self.elementView.iconImageSize];
    self.elementView.iconImageMarggin = [cm el_iconImageMargginWithOtherWise:self.elementView.iconImageMarggin];
    self.elementView.hiddenEmptyIconImage = [cm el_hiddenEmptyIconImageWithOtherWise:self.elementView.hiddenEmptyIconImage];
    self.elementView.titleText = cm.el_titleText;
    self.elementView.subTitleText = cm.el_subTitleText;
}
@end
